package com.asset.dispoal.business.bsAssetInstitutionBatch.service.commom;


import com.asset.dispoal.business.bsAssetInstitutionBatch.domain.db.entity.BsAssetInstitutionBatch;
import com.asset.dispoal.business.bsAssetInstitutionBatch.domain.db.mapper.BsAssetInstitutionBatchMapper;
import com.asset.dispoal.business.bsAssetInstitutionBatch.dto.BsAssetInstitutionBatchRequest;
import com.asset.dispoal.business.bsAssetInstitutionBatch.dto.BsAssetInstitutionBatchResponse;
import com.asset.dispoal.business.bsAssetInstitutionBatch.service.converter.BsAssetInstitutionBatchConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.util.CollectionUtil;
import tech.mhuang.pacebox.core.util.StringUtil;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.protocol.data.PageVO;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author qr
 * @date 2022/3/10$
 */
@Component
public class BsAssetInstitutionBatchQueryTree implements BaseCommand<BsAssetInstitutionBatchRequest, Result> {


    @Autowired
    BsAssetInstitutionBatchMapper bsAssetInstitutionBatchMapper;

    @Override
    public Result<PageVO<BsAssetInstitutionBatchResponse>> execute(BsAssetInstitutionBatchRequest bsAssetInstitutionBatchRequest) {
        BsAssetInstitutionBatch bsAssetInstitutionBatch = BsAssetInstitutionBatchConverter.INSTANCE.resToDo(bsAssetInstitutionBatchRequest);
        final List<BsAssetInstitutionBatch> bsAssetInstitutionBatches = bsAssetInstitutionBatchMapper.queryList(bsAssetInstitutionBatch);
        final List<BsAssetInstitutionBatchResponse> bsAssetInstitutionBatchResponses = BsAssetInstitutionBatchConverter.INSTANCE.doToRes(bsAssetInstitutionBatches);
        PageVO<BsAssetInstitutionBatchResponse> pageVO = new PageVO<>();
        if (CollectionUtil.isNotEmpty(bsAssetInstitutionBatchResponses) && bsAssetInstitutionBatchResponses.size() == 1) {
            pageVO.setTotalSize(1);
            pageVO.setResult(bsAssetInstitutionBatchResponses);

            return Result.ok(pageVO);
        }
        final List<BsAssetInstitutionBatchResponse> collect = bsAssetInstitutionBatchResponses.stream().filter(item -> StringUtil.isBlank(item.getParentId())).collect(Collectors.toList());
        bsAssetInstitutionBatchResponses.removeAll(collect);
        final List<BsAssetInstitutionBatchResponse> recursion = recursion(bsAssetInstitutionBatchResponses, collect);
        recursion.subList(
                bsAssetInstitutionBatchRequest.getStart() - 1,
                bsAssetInstitutionBatchRequest.getRows() - 1 > recursion.size() ?
                        recursion.size() : bsAssetInstitutionBatchRequest.getRows());
        pageVO.setTotalSize(recursion.size());
        pageVO.setResult(recursion);
        return Result.ok(pageVO);
    }


    private List<BsAssetInstitutionBatchResponse> recursion(List<BsAssetInstitutionBatchResponse> sysAddressResponses, List<BsAssetInstitutionBatchResponse> collect) {
        for (BsAssetInstitutionBatchResponse response : collect) {
            final List<BsAssetInstitutionBatchResponse> sysAddressResponseList = sysAddressResponses.stream().filter(itme -> itme.getParentId().equals(response.getId())).collect(Collectors.toList());
            response.setResponseList(sysAddressResponseList);
            recursion(sysAddressResponses, sysAddressResponseList);
        }
        return collect;
    }
}
